package test.example;

import com.bobo.data.DataStore;

import java.util.Arrays;

public class HeapData {

    public static void main(String[] args) {
         int[] ints = DataStore.getListRandom(10);
//        int [] ints={34, 10, 55, 32, 93, 78, 83, 38, 43, 33};
        System.out.println("args = " + Arrays.toString(ints));
        list2Heap(ints);
        System.out.println("args = " + Arrays.toString(ints));
    }
    public static void list2Heap(int [] data){
        int leng=data.length;
        for (int i = leng/2-1; i >=0 ; i--) {
            if(i==1){
                System.out.println("===");
            }
            int temp =data[i]; int k=i; int index=2*k+1;
            while (index<leng){
                if (index+1<leng){
                    if(data[index]<data[index+1]){
                        index=index+1;
                    }
                }
                if (data[index]>temp){
                    data[k]=data[index];
                    k=index;//子
                    index=2*k+1;
                }else {
                    break;
                }
            }
            data[k]=temp;
            System.out.println("args = " + Arrays.toString(data));
        }
    }


}
